//获取文件后缀名
import { utilJs } from "@/utils/common.js";
function getFileSuffix(fileName) {
  let lastIndex = fileName.lastIndexOf(".");
  let suffixStr = '';
  if (lastIndex != -1) {
    suffixStr = fileName.substr(lastIndex + 1);
  };
  return suffixStr;
};
//上传文件后缀匹配
function getFileSuffixRegexp() {
  let acceptType = [".docx", ".pdf", ".doc", ".xls", ".xlsx", ".ppt", ".ppsx", ".txt"];
  let extensionsRegExp = [];
  acceptType.forEach(ext => {
    let reg = ext.replace(new RegExp("[" + "/^$.*+?|()[]{}\\".replace(/./g, "\\$&") + "]", "g"), "\\$&");
    extensionsRegExp.push(reg);
  });
  return new RegExp("(" + extensionsRegExp.join("|") + ")$", "i");
}
//积分获取文件
/**
  * 积分的获取和取消
  * {
  *    sourceType:圈子积分获取行为( 2 动态获精选/取消精选  5 发布评论/删除评论 6 点赞动态/取消点赞动态) 7发布动态,
  *    handleType:	1 增加积分行为 2 减少积分行为,
  *    actId:对应行为(发布动态id/评论id)
  *    actType：对应行为类型 1 动态 2 评论
  *    circleId：圈子的id
  * }
*/
function togglePoints(data, circleId, ctx, isCustomTip) {
  return new Promise((resolve, reject) => {
    let sendData = {
      circleId: circleId,
      ...data
    }
    utilJs.postMethod(`${global.apiurl}circle/handleCirclePointsLog`, sendData, res => {
      console.log("res", res);
      if (res.points) {
        let text = "";
        let time = 0;
        if (data.actType == 2 && data.sourceType == 5) {
          if (data.handleType == 1) {
            text = "发布评论获积分 +" + res.points;
            time = 1500;
          }
        } else if (data.actType == 1 && data.sourceType == 6) {
          if (data.handleType == 1) {
            text = "点赞获积分 +" + res.points;
          }
        } else if (data.actType == 1 && data.sourceType == 7) {
          if (data.handleType == 1) {
            text = "+" + res.points + "积分";
          }
        }
        if (text && isCustomTip) {
          resolve(text)
        } else {
          setTimeout(() => {
            Vue.prototype.$showCjToast(text);
          }, time)
        }
      } else {
        resolve()
      }
    }, res => {
      reject()
    })
  });

}
//文件下载
function downloadFile(data, ctx) {
  let fullUrl = `${data.fullUrl}?attname=${data.fileName}`;
  if (utilJs.isIOSApp()) {
    window.webkit.messageHandlers.iosWechatDatumDownload.postMessage(
      JSON.stringify({ dataUrl: fullUrl })
    );
  } else if (utilJs.isAndroidApp()) {
    window.prompt(JSON.stringify({
      call: "appDownLoad",
      data: {
        dataUrl: data.fullUrl,
      },
    }));
  } else if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
    window.location.href = fullUrl;
  } else {
    ctx.$routerGo(ctx, "push", {
      path: "/homePage/datum/datumDownload",
      query: {
        url: fullUrl
      },
    });
  }
}
//计算进度转换格式
function calcProgressRatio(val) {
  let res = {
    leftDeg: 135,
    rightDeg: 135
  }
  if (!val) {
    res.leftDeg = 135;
    res.rightDeg = 135;
  } else if (val <= 0.5 && val > 0) {
    res.leftDeg = 135;
    res.rightDeg = 135 + val / 0.5 * 180
  } else {
    res.rightDeg = 315;
    res.leftDeg = 135 + (val - 0.5) / 0.5 * 180;
  }
  return res;
}


//添加任务成员
function addPlanMember(circleId, planId) {
  return new Promise((resolve, reject) => {
    utilJs.postMethod(`${global.apiurl}circle/addPlanMember`, {
      circleId: circleId,
      planId: planId
    }, () => {
      resolve();
    }, () => {
      reject();
    })
  })
}
export default {
  getFileSuffix,
  getFileSuffixRegexp,
  togglePoints,
  downloadFile,
  calcProgressRatio,
  addPlanMember
}